﻿@{
    ViewBag.Title = "ShowReturnOrderList";
    Layout = null;
}
<!DOCTYLE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.mobile.css" rel="stylesheet" />
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/scripts/layui-v2.5.6/layui/layui.all.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
</head>
<body>
    <form class="layui-form layui-row layui-col-space16">
        <div class="layui-form-item">
            <label class="layui-form-label">退货单编号：</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="rtCode" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
            <label class="layui-form-label">退货单名称：</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="rtName" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
            <label class="layui-form-label">采购订单编号：</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="soCode" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
            <label class="layui-form-label">供应商名称：</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="clientName" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>

            <button type="button" id="Showdata" class="layui-btn">查询</button>
            <button type="reset" id="restMdItemType" class="layui-btn">重置</button>
            <button type="button" class="layui-btn layui-btn-primary  layui-bg-blue" lay-on="layui-badge-dot">添加</button>
        </div>

        <table class="layui-hide" id="UntiInfoId" lay-filter="test">
        </table>
        <script type="text/html" id="toolDemo">
            <button type="button" class="layui-btn layui-bg-blue layui-btn-xs" lay-event="edit">编辑</button>
            <button type="button" class="layui-btn layui-bg-red layui-btn-xs" lay-event="delete">删除</button>
        </script>
    </form>

    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script src="~/libs/jquery/jquery.js"></script>

    <script>
        layui.use(['table'], function () {
            var table = layui.table;
            var form = layui.form;
            var measureName = layui.form.measureName;

            //显示
            ShowUnitInfo = function () {
                var rtCode = $("#rtCode").val();
                var rtName = $("#rtName").val();
                var soCode = $("#soCode").val();
                var clientName = $("#clientName").val();
                //执行一个 table 实例
                table.render({
                    elem: '#UntiInfoId'
                    , height: 520
                    , url: '/api/app/return-order/return-order-no'  //数据接口
                    , where: {
                        rtCode: rtCode,
                        rtName: rtName,
                        soCode: soCode,
                        clientName: clientName,
                    }
                    , title: '单位表'
                    , page: true //开启分页
                    , limit: 10
                    , limits: [10, 20, 30, 40]
                    , cols: [[ //表头
                        //{ type: 'checkbox', fixed: 'left' }//锁定第一列,
                        { field: 'RtCode', title: '退货单编号' },
                        { field: 'RtName', title: '退货单名称' },
                        { field: 'SoCode', title: '采购订单编号' },
                        { field: 'ClientId', title: '供应商编码' },
                        { field: 'ClientName', title: '供应商名称' },
                        { field: 'RtDate', title: '退货日期', },
                        { field: 'Status', title: '单据状态' },
                        { field: 'right', title: '操作', width: 150, templet: "#toolDemo" }
                    ]]
                });
            }
            ShowUnitInfo();

            //查询
            $("#Showdata").on("click", function () {
                ShowUnitInfo();
            })

            //添加
            layui.use(function () {
                var $ = layui.$;
                var layer = layui.layer;
                var util = layui.util;
                var form = layui.form;
                util.on('lay-on', {
                    'layui-badge-dot': function () {
                        layer.open({
                            type: 2,
                            area: ['500px', '550px'],
                            resize: false,
                            shadeClose: true,
                            title: '添加供应商退货',
                            content: `/Li/AddReturnOrder`,
                            btn: ['确定', '取消'],
                            yes: function (index, layero) {
                                //获取表单数据
                                var data = layer.getChildFrame('body', index).find('form').serialize();
                                console.log(data);
                                $.ajax({
                                    url: "/Li/AddReturnOrders",
                                    type: "get",
                                    async: true,
                                    data: data,
                                    contentType: "application/x-www-form-urlencoded",
                                    success: res => {
                                        if (res != null) {
                                            alert("添加成功");
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.layer.close(index);
                                            layer.closeAll();//关闭弹出层
                                            ShowUnitInfo();//刷新表格
                                        }
                                        else {
                                            alert("添加失败")
                                        }
                                    },
                                });
                                return false; // 阻止默认 form 跳转
                            },
                        });
                    }
                })
            });

            //表单操作
            table.on('tool(test)', function (obj) {
                //修改
                if (obj.event === 'edit') {
                    var data = obj.data;
                    console.log(obj.data.Id);
                    layer.open({
                        type: 2,
                        area: ['500px', '400px'],
                        resize: false,
                        shadeClose: true,
                        title: '修改车间',
                        content: `https://localhost:44311/JzyViews/UpdateUnitInfo?id=${data.Id}`,
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var data = layer.getChildFrame('body', index).find('form').serialize();
                            console.log(data);
                            $.ajax({
                                url: "/Li/UpdateUnitInfos",
                                type: "get",
                                async: true,
                                data: data,
                                contentType: "application/json",
                                // success: res => {
                                //     if (res != null) {
                                //         // var index = parent.layer.getFrameIndex(window.name);
                                //         // parent.layer.close(index);
                                //         alert("修改成功");
                                //         layer.closeAll();//关闭弹出层
                                //         ShowUnitInfo();//刷新表格
                                //     }
                                //     else {
                                //         alert("添加失败")
                                //     }
                                // },
                            });
                            alert("修改成功");
                            layer.closeAll();//关闭弹出层
                            ShowUnitInfo();//刷新表格
                            //return false; // 阻止默认 form 跳转
                        },

                    });
                }
                //删除
                else if (obj.event === 'delete') {
                    var data = obj.data//获取当前数据
                        , layEvent = obj.event;//获取lay-event获取对应的值
                    console.log(data.Id)
                    layer.confirm('真的删除供应商退货吗？', function (index) {
                        $.ajax({
                            url: "/Li/DeletReturnOrder?id=" + data.Id,
                            type: "get",
                        });
                        alert("删除成功");
                        obj.del();
                        layer.close(index);
                    })
                }
            });
        });
    </script>
</body>
</html>